package com.cj.controller;

import com.cj.po.Class;
import com.cj.po.PageInfo;
import com.cj.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户控制器类
 */
@Controller
public class ClassController {
    // 依赖注入
    @Autowired
    private ClassService classService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @GetMapping(value = "/findClass")
    public String findClass(String c_classname, Integer c_id, Integer c_classid, String c_counsellor,
                              Integer pageIndex, Integer pageSize, Model model) {

        PageInfo<Class> pi = classService.findPageInfo1(c_classname, c_id, c_classid, c_counsellor,
                 pageIndex, pageSize);
        model.addAttribute("pi", pi);
        model.addAttribute("c_classname", c_classname);
        return "class_list";
    }

    /**
     * 导出Excel
     * @return
     */
    @RequestMapping(value = "/exportclasslist", method = RequestMethod.POST)
    @ResponseBody
    public List<java.lang.Class> exportClass() {
        List<java.lang.Class> classList = classService.getAll();
        return classList;
    }

    /**
     * 删除学生信息
     */
    @RequestMapping("/deleteClass")
    @ResponseBody
    public String deleteClass(Integer c_id) {
        int s = classService.deleteClass(c_id);
        return "class_list";
    }

    /**
     * 添加学生信息
     */

    @RequestMapping(value = "/addClass", method = RequestMethod.POST)
    @ResponseBody
    public String addClass(@RequestBody Class cs) {
        int s = classService.addClass(cs);
        return "class_list";
    }

    /**
     * 修改学生信息
     */
    @RequestMapping(value = "/updateClass", method = RequestMethod.POST)
    public String updateClass(Class cs) {
        int s = classService.updateClass(cs);
        return "redirect:/findClass";
    }


    @RequestMapping("/findClassById")
    public String findStudentById(Integer c_id, HttpSession session) {

        java.lang.Class s = classService.findClassById(c_id);
        session.setAttribute("s", s);
        return "class_edit";

    }
}
